<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1;

use App\Controller\Admin\AdminController;
use App\Model\RbacMenu;
use App\Model\RbacPermissionHasSource;
use App\Model\RbacRoleHasPermission;
use App\Repositories\AdminInfoRepository;
use App\Repositories\RbacMenuRepository;
use App\Request\AdminLoginRequest;
use App\Request\AdminRegisterRequest;
use App\Request\NullRequest;
use App\Request\StoreAdminInfoRequest;
use App\Services\AdminService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JwtAuthMiddleware;
use App\Request\StoreUpdatePwdRequest;
use Phper666\JwtAuth\Jwt;

class AuthController extends AdminController
{
    /**
     * @Inject
     * @var Jwt
     */
    protected $jwt;

    /**
     * @Inject
     * @var AdminService
     */
    protected $service;

    public function login(AdminLoginRequest $request)
    {
        $return = $this->service->login($request->all());
        if($return['success']){
            return $this->response->success($return['data']);
        }
        return $this->response->error(10000,$return['msg']);
    }

    public function register(AdminRegisterRequest $request){
        $return = $this->service->register($request->all());
        if($return['success']){
            return $this->response->success($return['data']);
        }
        return $this->response->error(10000,$return['msg']);
    }

    public function refreshToken()
    {
        $token = $this->jwt->refreshToken();
        return $this->response->success([
            'access_token' => (string) $token,
            'expires_in' => $this->jwt->getTTL()
        ]);
    }

    public function logOut()
    {
        try {
            $this->jwt->logout();
        } catch (\Exception $e) {

        }
        return $this->response->success([
           'msg' => '退出成功'
        ]);
    }

    public function updatePwd(StoreUpdatePwdRequest $request){
        $params = $request->all();
        $admin = $this->request->getAttribute('admin');
        $old_pwd = $params['old_pwd'];
        if (!password_verify($old_pwd,$admin->password)) {
            return $this->response->error(10010,'密码不正确');
        }
        $admin->password = password_hash($params['pwd'],PASSWORD_BCRYPT);
        if(!$admin->save()){
            return $this->response->error(10010,'密码保存失败');
        }
        return $this->response->success([]);
    }

    public function updateInfo(StoreAdminInfoRequest $request){
        $params = $request->all();
        $admin = $this->request->getAttribute('admin');
        $info = AdminInfoRepository::getInstance()->one(['id'],[[
            'field' => 'admin_id',
            'value' => $admin->id
        ]]);
        $basic_params=[
            'admin_id' => $admin->id,
            'id' => $info?$info->id:0
        ];
        $result = AdminInfoRepository::getInstance()->store($basic_params+$params);
        if(!$result['success']){
            return $this->response->error(10020,$result['msg']);
        }
        return $this->response->success([]);
    }

    public function getInfo(){
        $admin = $this->request->getAttribute('admin');
        $info = AdminInfoRepository::getInstance()->one(['id','real_name','contact_tel'],[[
            'field' => 'admin_id',
            'value' => $admin->id
        ]]);
        return $this->response->success($info);
    }
}